/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.CustomerBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author MrKoi
 */
public class ControllerProfile extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        CustomerBean CTbean=new CustomerBean();
       
       
      if(request.getParameter("update")!=null)
      {            
            if(request.getParameter("update").equals("Change Password"))
            {
                String username=request.getParameter("user");
                String passold=request.getParameter("passOld");
                String passNew=request.getParameter("passNew");
                boolean result=CTbean.UpdateCustomerPass(username,passold,passNew);
                if(result ==true)
                {
                    session.setAttribute("para","Change password success!");
                    response.sendRedirect("include/Notice.jsp");
                }
                else
                {
                   // out.println("<a href='index.jsp?page=Profile'>Insert fail back...!</a>");
                    out.println("<script>alert('Please enter password old correct!');javascript:window.history.back(-1);</script>");
                }
            }
      }
      if(request.getParameter("update")!=null)
      {            
            if(request.getParameter("update").equals("Update Information"))
            {
                String username=request.getParameter("user");
                String fname=request.getParameter("fullname");
                String gender=request.getParameter("gender");
                String email=request.getParameter("email");
                String Country=request.getParameter("country");
                String address=request.getParameter("address");
                String phone=request.getParameter("phone");
                boolean result=CTbean.UpdateCustomerNoPass(username,fname,gender,email,Country,address,phone);
                if(result ==true)
                {
                    session.setAttribute("para","Update profile success!");
                    response.sendRedirect("include/Notice.jsp");
                }
                else
                {
                    //out.println("<a href='index.jsp?page=Profile'>Insert fail back...!</a>");
                    out.println("<script>alert('Error data! Please input information correct!');javascript:window.history.back(-1);</script>");
                }
            }
      }
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
